#include "Ctrl/AddNewRootRequirementCtrlCmd.hpp"
#include "Ctrl/ControlCenter.hpp"
#include "Storage/ConnectionConfig.hpp"
#include "Storage/StorageFactory.hpp"
#include "Storage/StorageCommand.hpp"
#include "Storage/ProjectStorage.hpp"
#include "Storage/PackageStorage.hpp"
#include "Storage/RequirementsStorage.hpp"
#include "Model/Package.hpp"
#include "Model/Requirement.hpp"
#include <cassert>

namespace Ctrl
{


AddNewRootRequirementCtrlCmd::AddNewRootRequirementCtrlCmd( 
		ControlCenter& ctrlCenter, Model::Package* pParentPackage, Model::Requirement* pRootRequirement )
	: ControlCommand( ctrlCenter)
	, pParentPackage_( pParentPackage)
	, pRootRequirement_( pRootRequirement)
{
	assert( NULL != pParentPackage);
	assert( NULL != pRootRequirement);
	assert( !pRootRequirement->hasParent());
}

// ---------------------------------------------------------------------------------------------------------------------

void AddNewRootRequirementCtrlCmd::execute()
{
	pParentPackage_->addRequirement( pRootRequirement_);
	rCtrlCenter_.packageController().announceRootRequirementAdded( pParentPackage_, pRootRequirement_);
	// Take care of the storage:
	Storage::StorageFactory& storageFactory = Ctrl::ControlCenter::instance().prjController().storageFactory();
	storageFactory.getRequirementStorage().pushCommand( storageFactory.createRootRequirementCreationCommand( *pRootRequirement_));
}

// ---------------------------------------------------------------------------------------------------------------------

void AddNewRootRequirementCtrlCmd::undo()
{
	pParentPackage_->removeRequirement( pRootRequirement_);
	rCtrlCenter_.packageController().announceRootRequirementRemoved( pParentPackage_, pRootRequirement_);
	// Take care of the storage:
	Storage::StorageFactory& storageFactory = Ctrl::ControlCenter::instance().prjController().storageFactory();
	storageFactory.getRequirementStorage().pushCommand( storageFactory.createRootRequirementRemovalCommand( *pRootRequirement_, pParentPackage_));
}

} // namespace Ctrl

